using Microsoft.AspNetCore.Mvc;
using SqlsugarService.Application.DTOs.LMZWorkSchedule;
using SqlsugarService.Application.IService.LMZWorkSchedule;
using SqlsugarService.Application.Until;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SqlsugarService.API.Controllers.LMZWorkScheduleController
{
    /// <summary>
    /// LMZ质检系统控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class LMZWorkScheduleController : ControllerBase
    {
        private readonly ILMZWorkScheduleService _lmzWorkScheduleService;

        /// <summary>
        /// 构造函数，注入LMZ质检系统服务
        /// </summary>
        /// <param name="lmzWorkScheduleService">LMZ质检系统业务服务</param>
        public LMZWorkScheduleController(ILMZWorkScheduleService lmzWorkScheduleService)
        {
            _lmzWorkScheduleService = lmzWorkScheduleService;
        }

        /// <summary>
        /// 分页获取质检系统基础列表
        /// </summary>
        /// <param name="orderName">工单名称过滤</param>
        /// <param name="processName">工序名称过滤</param>
        /// <param name="status">状态过滤</param>
        /// <param name="startTime">开始时间过滤</param>
        /// <param name="endTime">结束时间过滤</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>分页的质检基础列表</returns>
        [HttpGet("basic-list")]
        public async Task<ApiResult<PageResult<List<LMZWorkScheduleListDto>>>> LMZGetWorkSchedulePaged(
            [FromQuery] string? orderName,
            [FromQuery] string? status,
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 20)
        {
            try
            {
                var queryDto = new LMZWorkScheduleQueryDto
                {
                    OrderName = orderName,
                    Status = status,
                    PageIndex = pageIndex,
                    PageSize = pageSize
                };

                return await _lmzWorkScheduleService.LMZGetWorkSchedulePagedAsync(queryDto);
            }
            catch (Exception ex)
            {
                return ApiResult<PageResult<List<LMZWorkScheduleListDto>>>.Fail(
                    $"服务器异常: {ex.Message}", ResultCode.Error);
            }
        }

        /// <summary>
        /// 分页获取质检系统详细列表
        /// </summary>
        /// <param name="orderName">工单名称过滤</param>
        /// <param name="processName">工序名称过滤</param>
        /// <param name="status">状态过滤</param>
        /// <param name="startTime">开始时间过滤</param>
        /// <param name="endTime">结束时间过滤</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>分页的质检详细列表</returns>
        [HttpGet("detail-list")]
        public async Task<ApiResult<PageResult<List<LMZWorkScheduleDetailDto>>>> LMZGetWorkScheduleDetailPaged(
            [FromQuery] string? orderName,
            [FromQuery] string? status,
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 20)
        {
            try
            {
                var queryDto = new LMZWorkScheduleQueryDto
                {
                    OrderName = orderName,
                    Status = status,
                    PageIndex = pageIndex,
                    PageSize = pageSize
                };

                return await _lmzWorkScheduleService.LMZGetWorkScheduleDetailPagedAsync(queryDto);
            }
            catch (Exception ex)
            {
                return ApiResult<PageResult<List<LMZWorkScheduleDetailDto>>>.Fail(
                    $"服务器异常: {ex.Message}", ResultCode.Error);
            }
        }

        /// <summary>
        /// 根据任务ID获取质检详情
        /// </summary>
        /// <param name="taskId">任务ID</param>
        /// <returns>质检详情</returns>
        [HttpGet("detail/{taskId}")]
        public async Task<ApiResult<LMZWorkScheduleDetailDto>> LMZGetWorkScheduleById(Guid taskId)
        {
            try
            {
                return await _lmzWorkScheduleService.LMZGetWorkScheduleByIdAsync(taskId);
            }
            catch (Exception ex)
            {
                return ApiResult<LMZWorkScheduleDetailDto>.Fail(
                    $"服务器异常: {ex.Message}", ResultCode.Error);
            }
        }

        /// <summary>
        /// 使用POST方式进行复杂查询（支持更多查询条件）
        /// </summary>
        /// <param name="queryDto">查询参数</param>
        /// <returns>分页的质检基础列表</returns>
        [HttpPost("basic-list/query")]
        public async Task<ApiResult<PageResult<List<LMZWorkScheduleListDto>>>> LMZGetWorkSchedulePagedByPost(
            [FromBody] LMZWorkScheduleQueryDto queryDto)
        {
            try
            {
                return await _lmzWorkScheduleService.LMZGetWorkSchedulePagedAsync(queryDto);
            }
            catch (Exception ex)
            {
                return ApiResult<PageResult<List<LMZWorkScheduleListDto>>>.Fail(
                    $"服务器异常: {ex.Message}", ResultCode.Error);
            }
        }

        /// <summary>
        /// 使用POST方式进行复杂查询（支持更多查询条件）
        /// </summary>
        /// <param name="queryDto">查询参数</param>
        /// <returns>分页的质检详细列表</returns>
        [HttpPost("detail-list/query")]
        public async Task<ApiResult<PageResult<List<LMZWorkScheduleDetailDto>>>> LMZGetWorkScheduleDetailPagedByPost(
            [FromBody] LMZWorkScheduleQueryDto queryDto)
        {
            try
            {
                return await _lmzWorkScheduleService.LMZGetWorkScheduleDetailPagedAsync(queryDto);
            }
            catch (Exception ex)
            {
                return ApiResult<PageResult<List<LMZWorkScheduleDetailDto>>>.Fail(
                    $"服务器异常: {ex.Message}", ResultCode.Error);
            }
        }

        /// <summary>
        /// 分页获取质检记录列表（只显示已质检状态）
        /// </summary>
        /// <param name="orderName">工单名称过滤</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>分页的质检记录列表</returns>
        [HttpGet("inspection-records")]
        public async Task<ApiResult<PageResult<List<LMZWorkScheduleListDto>>>> LMZGetInspectionRecordsPaged(
            [FromQuery] string? orderName,
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 20)
        {
            try
            {
                var queryDto = new LMZWorkScheduleQueryDto
                {
                    OrderName = orderName,
                    PageIndex = pageIndex,
                    PageSize = pageSize
                };

                return await _lmzWorkScheduleService.LMZGetInspectionRecordsPagedAsync(queryDto);
            }
            catch (Exception ex)
            {
                return ApiResult<PageResult<List<LMZWorkScheduleListDto>>>.Fail(
                    $"服务器异常: {ex.Message}", ResultCode.Error);
            }
        }

        /// <summary>
        /// 使用POST方式获取质检记录列表（只显示已质检状态）
        /// </summary>
        /// <param name="queryDto">查询参数</param>
        /// <returns>分页的质检记录列表</returns>
        [HttpPost("inspection-records/query")]
        public async Task<ApiResult<PageResult<List<LMZWorkScheduleListDto>>>> LMZGetInspectionRecordsPagedByPost(
            [FromBody] LMZWorkScheduleQueryDto queryDto)
        {
            try
            {
                return await _lmzWorkScheduleService.LMZGetInspectionRecordsPagedAsync(queryDto);
            }
            catch (Exception ex)
            {
                return ApiResult<PageResult<List<LMZWorkScheduleListDto>>>.Fail(
                    $"服务器异常: {ex.Message}", ResultCode.Error);
            }
        }

        /// <summary>
        /// 获取质检方案列表
        /// </summary>
        /// <returns>质检方案列表</returns>
        [HttpGet("quality-inspection-plans")]
        public async Task<ApiResult<List<QualityInspectionDto>>> LMZGetQualityInspectionPlans()
        {
            try
            {
                return await _lmzWorkScheduleService.LMZGetQualityInspectionPlansAsync();
            }
            catch (Exception ex)
            {
                return ApiResult<List<QualityInspectionDto>>.Fail(
                    $"服务器异常: {ex.Message}", ResultCode.Error);
            }
        }
    }
}